﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using Ice.Lib.Framework;
using System.Collections;
using System.IO;
using System.Data;
using Ice.Lib.Searches;

namespace Epicor.Pub
{
    /// <summary>DLL反射类</summary>
    public class AdpAssembly
    {
        public static Hashtable adapter = new Hashtable();
        private Type Type = null;
        private string Name = null;
        /// <summary>DLL反射类构造函数</summary>
        public AdpAssembly(EpiTransaction otran, string name)
        {
            Name = name;
            if (adapter[Name] == null)
            {
                string dllfile = "Erp.Adapters." + Name + ".dll";
                string className = "Erp.Adapters." + Name + "Adapter";
                if (!File.Exists(Application.StartupPath + @"\" + dllfile)) {
                    dllfile = "Ice.Adapters." + Name + ".dll";
                    className = "Ice.Adapters." + Name + "Adapter";
                }
                Type = Assembly.LoadFrom(Application.StartupPath + @"\" + dllfile).GetType(className);
                adapter[Name] = Activator.CreateInstance(Type, new object[] { otran });
            }
        }

        /// <summary>调用funName方法,obj数组为参数,types为参数类型</summary>
        public object InvokeFun(string funName, object[] obj = null, Type[] types = null)
        {
            MethodInfo Fun;
            if (types == null) Fun = Type.GetMethod(funName);
            else Fun = Type.GetMethod(funName, types);
            return Fun.Invoke(adapter[Name], obj);
        }
        public void BOConnect()
        {
            InvokeFun("BOConnect");
        }
        public void Update()
        {
            InvokeFun("Update");
        }
        public void Dispose()
        {
            InvokeFun("Dispose");
        }
        public bool GetByID(object[] obj = null, Type[] types = null)
        {
            return (bool)InvokeFun("GetByID", obj, types);
        }
        public DataSet GetCurrentDataSet(object[] obj = null, Type[] types = null)
        {
            return (DataSet)InvokeFun("GetCurrentDataSet", new object[] { DataSetMode.RowsDataSet }, new Type[] { typeof(DataSetMode) });
        }
    }
}
